package com.snail.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.snail.mapper.BizMediaFileMapper;
import com.snail.model.entity.NoteQueryRequest;
import com.snail.model.entity.NoteSaveRequest;
import com.snail.model.pojo.BizMediaFile;
import com.snail.model.vo.UserVo;
import com.snail.service.BizMediaDocService;
import com.snail.model.pojo.BizMediaDoc;
import com.snail.mapper.BizMediaDocMapper;
import com.snail.service.BizMediaFileService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import java.time.LocalDateTime;

/**
* @author Monica
* @description 针对表【biz_media_doc(文档表)】的数据库操作Service实现
* @createDate 2025-07-16 10:22:25
*/
@Service
@Slf4j
public class BizMediaDocServiceImpl extends ServiceImpl<BizMediaDocMapper, BizMediaDoc>
    implements BizMediaDocService {

    @Autowired
    private BizMediaDocMapper bizMediaDocMapper;
    @Autowired
    private BizMediaFileService bizMediaFileService;

    @Autowired
    private BizMediaFileMapper bizMediaFileMapper;

    /**
     * 新增文档
     * @param bizMediaDoc 文档对象
     */
    @Override
    public void addDoc(BizMediaDoc bizMediaDoc) {
        bizMediaDocMapper.insert(bizMediaDoc);
    }

    /**
     * 根据id查询文档
     * @param id 文档id
     * @return
     */
    @Override
    public BizMediaDoc getDocById(Integer id) {
        return bizMediaDocMapper.selectById(id);
    }

    /**
     * 获取多条件查询文档列表
     * @param bizMediaDoc 文档对象
     * @return
     */
    @Override
    public List<BizMediaDoc> getDocList(BizMediaDoc bizMediaDoc) {
        return bizMediaDocMapper.selectList(bizMediaDoc);
    }

    /**
     * 根据id更新文档
     * @param bizMediaDoc 文档对象
     */
    @Override
    public void updateDocById(BizMediaDoc bizMediaDoc) {
        bizMediaDocMapper.updateById(bizMediaDoc);
    }


    /**
     * 根据用户id获取文档列表
     * 分页查询
     * @param userVo
     * @return
     */
    @Override
    public List<BizMediaDoc> getDocListByUserId(UserVo userVo) {
        /// 分页查询文档列表
        Page<BizMediaDoc> page = new Page<>(userVo.getCurrent(), userVo.getPageSize());
        LambdaQueryWrapper<BizMediaDoc> queryWrapper = new LambdaQueryWrapper<>();
        //添加判断条件deleted=0show deleted=1
        queryWrapper.eq(BizMediaDoc::getCreateBy, userVo.getUserId());
        queryWrapper.eq(BizMediaDoc::getFileType, 1);
        List<BizMediaDoc> docList = bizMediaDocMapper.selectPage(page, queryWrapper).getRecords();

        // 遍历文档列表，关联视频信息
        for (BizMediaDoc doc : docList) {
            if (doc.getVideoId() != null) {
                // 根据 videoId 查询视频信息
                BizMediaFile mediaFile = bizMediaFileMapper.selectById(doc.getVideoId());
                doc.setMediaFile(mediaFile); // 关联到文档对象
            }
        }

        return docList;
    }

    /**
     * 根据用户id、视频id查询文档
     * 先根据用户id查询文档列表，再根据视频id查询文档
     * @param userVo
     * @return
     */
    @Override
    public BizMediaDoc getDocByUserIdAndOutlineId(UserVo userVo) {
        LambdaQueryWrapper<BizMediaDoc> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BizMediaDoc::getCreateBy, userVo.getUserId());
        //根据视频id查询文档  固定笔记1
        queryWrapper.eq(BizMediaDoc::getFileType, 1);
//        queryWrapper.eq(BizMediaDoc::getVideoId, userVo.getVideoId());
        queryWrapper.eq(BizMediaDoc::getOutlineId, userVo.getOutlineId());
        List<BizMediaDoc> bizMediaDocs = bizMediaDocMapper.selectList(queryWrapper);
        if (bizMediaDocs.isEmpty()) {
            return null;
        }
        return bizMediaDocs.get(0);
    }

    @Override
    public String getContentById(Integer id) {
        BizMediaDoc bizMediaDoc = this.getById(id);
        return bizMediaDoc.getContent();
    }

    @Override
    public void saveWithoutAutoFill(BizMediaDoc bizMediaDoc) {
        bizMediaDoc.setCreateBy(0L);    // 默认更新人，由定时任务填充的
        bizMediaDoc.setFileType(0);     // 默认文件类型:字幕，由定时任务填充的
        bizMediaDoc.setCreateTime(LocalDateTime.now());
        bizMediaDoc.setUpdateTime(LocalDateTime.now());
        baseMapper.insert(bizMediaDoc);
    }

    @Override
    public void saveNote(NoteSaveRequest noteSaveRequest) {
        BizMediaDoc bizMediaDoc = new BizMediaDoc();
        BeanUtils.copyProperties(noteSaveRequest, bizMediaDoc);
        bizMediaDoc.setFileType(2);
        boolean saved = this.saveOrUpdate(bizMediaDoc);
        if (!saved) {
            throw new RuntimeException("保存笔记失败");
        }
    }

    @Override
    public BizMediaDoc getNoteByRequest(NoteQueryRequest noteQueryRequest) {
        if (noteQueryRequest.getId() != null && noteQueryRequest.getId() != 0) {
            return this.getById(noteQueryRequest.getId());
        }
        return this.getOne(new LambdaQueryWrapper<BizMediaDoc>()
                .eq(BizMediaDoc::getCreateBy, noteQueryRequest.getCreateBy())
                .eq(BizMediaDoc::getFileType, 2)
                .eq(BizMediaDoc::getOutlineId, noteQueryRequest.getOutlineId()));
    }

    @Override
    public List<BizMediaDoc> getNotesByUser(NoteQueryRequest noteQueryRequest) {
        Long userId = noteQueryRequest.getCreateBy();
        return this.list(new LambdaQueryWrapper<BizMediaDoc>()
                .eq(BizMediaDoc::getCreateBy, userId)
                .eq(BizMediaDoc::getFileType, 2));
    }
}




